package com.woniu.user.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.user.controller.form.CoachApplyForm;
import com.woniu.user.dao.VenueDao;
import com.woniu.user.model.Account;
import com.woniu.user.model.Coach;
import com.woniu.user.model.CoachApply;
import com.woniu.user.dao.CoachApplyDao;
import com.woniu.user.model.Venue;
import com.woniu.user.service.CoachApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.user.service.CoachService;
import com.woniu.user.util.BeanCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 教练登记申请表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@Service
public class CoachApplyServiceImpl extends ServiceImpl<CoachApplyDao, CoachApply> implements CoachApplyService {
    @Resource
    private CoachApplyDao coachApplyDao;
    @Resource
    private VenueDao venueDao;
    @Resource
    private CoachService coachService;

    @Override
    public void beCoach(CoachApplyForm coachApplyForm, Account account) {
        //雪花ID生成预约单号no
        Snowflake snowflake = new Snowflake(1, 1);
        String no = snowflake.nextIdStr();

        //通过账号信息完善申请信息
        CoachApply coachApply = BeanCopyUtil.copyObject(coachApplyForm, CoachApply.class);
        coachApply.setGender(account.getGender());
        coachApply.setNo(no);
        coachApply.setPhone(account.getPhone());
        Venue venue = venueDao.selectById(coachApply.getVenueId());
        coachApply.setVenueName(venue.getName());
        coachApply.setAccountId(account.getAccountId());
        coachApply.setState("审核中");
        //存入数据库
        save(coachApply);
    }

    @Override
    public void successBeCoach(Integer coachApplyId) {
        CoachApply coachApply = getById(coachApplyId);
        Coach coach = BeanCopyUtil.copyObject(coachApply, Coach.class);
        coachService.beCoach(coach);
    }

    @Override
    public List<CoachApply> query(Integer userId) {
        List<CoachApply> list = coachApplyDao.selectList(new QueryWrapper<CoachApply>().eq("account_id", userId));
        return list;
    }
}
